package com.zzyl.serve.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.member.dto.servceListDTO;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有护理项目（Day6 - 课堂1）
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> queryAll() {
        // select id value, name label from nursing_project where status = 1
        // 一、mybatisplus方式
        // 1、查询数据库
        /*List<NursingProject> list = this.lambdaQuery()
                .select(NursingProject::getId, NursingProject::getName)
                .eq(NursingProject::getStatus, 1)
                .list();

        // 2、将数据对象集合转换成vo集合
        List<NursingProjectVo> voList = list.stream().map(project -> {
            NursingProjectVo nursingProjectVo = new NursingProjectVo();
            nursingProjectVo.setLabel(project.getName());
            nursingProjectVo.setValue(String.valueOf(project.getId()));
            return nursingProjectVo;
        }).collect(Collectors.toList());*/

        // 2、mybatis原生方式
        List<NursingProjectVo> voList = nursingProjectMapper.queryAll();
        return voList;
    }

    @Override
    public IPage<NursingProject> servceNursingProjectList(servceListDTO servceListDTO) {
        //查询条件
        LambdaQueryWrapper<NursingProject> queryWrapper = new LambdaQueryWrapper<NursingProject>()
                .eq(ObjectUtil.isNotNull(servceListDTO.getStatus()), NursingProject::getStatus, servceListDTO.getStatus())
                .like(StrUtil.isNotBlank(servceListDTO.getName()), NursingProject::getName, servceListDTO.getName());

        //调用plus中的Page完成分页查询，返回的是IPage对象
        return this.page(Page.of(servceListDTO.getPageNum(), servceListDTO.getPageSize()), queryWrapper);


    }
}
